package ru.silviasaint.fridge.client.widgets;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;
import ru.silviasaint.fridge.client.events.PageChangeEvent;
import ru.silviasaint.fridge.client.utils.GlobalEventBusHolder;

/**
 * Created by Dmitry.
 * User: jetblack20
 * Date: 4/7/12
 * Time: 12:48 PM
 */
public class Paginator extends Composite {

    private EventBus eventBus;
    private int activePage;

    interface PaginatorUiBinder extends UiBinder<Widget, Paginator> {
    }

    private static PaginatorUiBinder ourUiBinder = GWT.create(PaginatorUiBinder.class);

    @UiField
    HTMLPanel mainPanel;


    public Paginator(int activePage) {
        this.eventBus = GlobalEventBusHolder.getEventBus();
        this.activePage = activePage;
        initWidget(ourUiBinder.createAndBindUi(this));
        for (int i = 1; i < 10; i++) {
            mainPanel.add(getPagedAnchor(i));
        }
    }



    private Anchor getPagedAnchor(final int pageNumber) {
        Anchor anchor = new Anchor(String.valueOf(pageNumber));
        anchor.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                PageChangeEvent ev = new PageChangeEvent();
                ev.setPage(pageNumber);
                eventBus.fireEvent(ev);
            }
        });
        return anchor;
    }


}